package NGUYENVANDOAN.XMLEXCERCISES;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.toedter.calendar.JDateChooser;

public class VIEWS extends JFrame {

    private final JPanel contentPane;
    private final JTextField txtMaSV;
    private final JTextField txtTenSV;
    private final JTextField txtKQ;
    private final JTextField txtDienThoai;
    private final JTextField txtEmail;
    private final JTextField txtCMND;
    JComboBox cboGioiTinh;
    private final List<List<Object>> lisValue = new ArrayList<List<Object>>();
    private int iCount = 0;
    private final JDateChooser txtNgaySinh;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    VIEWS frame = new VIEWS();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public VIEWS() {
        // lisStudent = new ArrayList<Students>();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 597, 403);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBorder(new TitledBorder(null, "X\u1EED l\u00FD t\u00EDnh to\u00E1n", TitledBorder.LEADING,
                TitledBorder.TOP, null, null));
        panel.setBounds(10, 11, 568, 353);
        contentPane.add(panel);

        JLabel label = new JLabel("Mã Sinh Viên:");
        label.setFont(new Font("Cambria", Font.PLAIN, 14));
        label.setBounds(10, 19, 94, 18);
        panel.add(label);

        JLabel label_1 = new JLabel("Họ và Tên:");
        label_1.setFont(new Font("Cambria", Font.PLAIN, 14));
        label_1.setBounds(10, 55, 94, 18);
        panel.add(label_1);

        JLabel label_2 = new JLabel("Giới Tính:");
        label_2.setFont(new Font("Cambria", Font.PLAIN, 14));
        label_2.setBounds(10, 97, 104, 18);
        panel.add(label_2);

        JLabel label_3 = new JLabel("Kết Quả:\r\n");
        label_3.setFont(new Font("Cambria", Font.PLAIN, 14));
        label_3.setBounds(10, 248, 94, 18);
        panel.add(label_3);

        txtMaSV = new JTextField();
        txtMaSV.setColumns(10);
        txtMaSV.setBounds(112, 19, 224, 21);
        panel.add(txtMaSV);

        txtTenSV = new JTextField();
        txtTenSV.setColumns(10);
        txtTenSV.setBounds(112, 55, 446, 21);
        panel.add(txtTenSV);

        cboGioiTinh = new JComboBox();
        cboGioiTinh.setModel(new DefaultComboBoxModel(new String[] { "Nam", "Nữ" }));
        cboGioiTinh.setFont(new Font("Cambria", Font.PLAIN, 14));
        cboGioiTinh.setBounds(112, 97, 130, 20);
        panel.add(cboGioiTinh);

        txtKQ = new JTextField();
        txtKQ.setColumns(10);
        txtKQ.setBounds(114, 234, 444, 74);
        panel.add(txtKQ);

        JButton btnThemThongTin = new JButton("Thêm Thông Tin");
        btnThemThongTin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // 1. Khởi tạo đối tượng Sinh Viên
                List<Object> lis = new ArrayList<Object>();

                // 2. Khởi tạo thông tin cho đối tượng Sinh Viên
                lis.add(Integer.parseInt(txtMaSV.getText()));
                lis.add(txtTenSV.getText());
                lis.add(cboGioiTinh.getSelectedItem().toString());

                SimpleDateFormat dateFormat = new SimpleDateFormat();
                String birthday = dateFormat.format(txtNgaySinh.getDate()); // dd/MM/yyyy
                lis.add(birthday);
                lis.add(txtEmail.getText());
                lis.add(Integer.parseInt(txtDienThoai.getText()));
                lis.add(txtCMND.getText());

                // 3. Đưa List<Object> vào List<List<Object>>
                lisValue.add(lis);                
                txtKQ.setText("Có: " + lisValue.size() + " Sinh Viên trong danh sách!");

                // 4. Clear
                txtMaSV.setText("");
                txtTenSV.setText("");
                txtCMND.setText("");
                txtDienThoai.setText("");
                txtEmail.setText("");
                txtNgaySinh.cleanup();
            }
        });
        btnThemThongTin.setFont(new Font("Cambria", Font.PLAIN, 12));
        btnThemThongTin.setBounds(435, 319, 123, 23);
        panel.add(btnThemThongTin);

        JLabel label_4 = new JLabel("Ngày Sinh:");
        label_4.setFont(new Font("Cambria", Font.PLAIN, 14));
        label_4.setBounds(10, 134, 94, 18);
        panel.add(label_4);

        JLabel label_5 = new JLabel("Điện thoại:");
        label_5.setFont(new Font("Cambria", Font.PLAIN, 14));
        label_5.setBounds(10, 171, 94, 18);
        panel.add(label_5);

        txtDienThoai = new JTextField();
        txtDienThoai.setColumns(10);
        txtDienThoai.setBounds(112, 171, 216, 21);
        panel.add(txtDienThoai);

        JLabel label_6 = new JLabel("Email:");
        label_6.setFont(new Font("Cambria", Font.PLAIN, 14));
        label_6.setBounds(338, 171, 47, 18);
        panel.add(label_6);

        txtEmail = new JTextField();
        txtEmail.setColumns(10);
        txtEmail.setBounds(395, 171, 163, 21);
        panel.add(txtEmail);

        JLabel label_7 = new JLabel("Số CMND:");
        label_7.setFont(new Font("Cambria", Font.PLAIN, 14));
        label_7.setBounds(10, 207, 94, 18);
        panel.add(label_7);

        txtCMND = new JTextField();
        txtCMND.setColumns(10);
        txtCMND.setBounds(112, 207, 216, 21);
        panel.add(txtCMND);

        JButton btnWriteXML = new JButton("Ghi dữ liệu vào XML");
        btnWriteXML.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // 1. Khai báo List Field
                List<Object> lisField = new ArrayList<Object>();
                lisField.add("RollNo");
                lisField.add("FullName");
                lisField.add("Sex");
                lisField.add("NgaySinh");
                lisField.add("Email");
                lisField.add("Phone");
                lisField.add("CMND");
                
                //2. Thiết lập các tham số cấu hình cho thực thi nghiệp vụ
                String rootNode = "iMIC";
                String elementNode = "Student";
                JFileChooser objFile = new JFileChooser();
                objFile.setCurrentDirectory(new java.io.File("."));
                objFile.setDialogTitle("Chọn file: ");
                objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                String[] EXTENSION = new String[] { "xml" };
                FileNameExtensionFilter filter = new FileNameExtensionFilter("XML files", EXTENSION);
                objFile.setFileFilter(filter);
                objFile.setMultiSelectionEnabled(false);

                int returnVal = objFile.showSaveDialog(getParent());
                String ParentPath = "";
                String FileName = "";
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    ParentPath = objFile.getSelectedFile().getParent();
                    FileName = objFile.getSelectedFile().getName();
                }
                String path = ParentPath + "/" + FileName;
                
                //3. Khai báo và khởi tạo Controller
                CONTROLLERS objController = new CONTROLLERS();
                
                try {
                    // 3. Write dữ liệu xuống files XML                    
                    boolean bResults = objController
                                .WriteDataOf(lisField, lisValue, rootNode, 
                                        elementNode, path, true);
                    if (bResults)
                        JOptionPane.showMessageDialog(getParent(), 
                                "Ghi dữ liệu thành công!!!", "Thông báo",
                                JOptionPane.INFORMATION_MESSAGE);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(getParent(), "Err: " + e.getMessage());
                }
            }
        });
        btnWriteXML.setBounds(295, 320, 130, 23);
        panel.add(btnWriteXML);

        txtNgaySinh = new JDateChooser();
        txtNgaySinh.setDateFormatString("dd/MM/yyyy");
        txtNgaySinh.setBounds(112, 132, 216, 20);
        panel.add(txtNgaySinh);

        JButton btnGhiDuLieuNoiTiep = new JButton("Ghi dữ liệu nối tiếp vào XML");
        btnGhiDuLieuNoiTiep.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
             // 1. Khai báo List Field
                List<Object> lisField = new ArrayList<Object>();
                lisField.add("RollNo");
                lisField.add("FullName");
                lisField.add("Sex");
                lisField.add("NgaySinh");
                lisField.add("Email");
                lisField.add("Phone");
                lisField.add("CMND");

                //????
            }
        });
        btnGhiDuLieuNoiTiep.setBounds(124, 320, 166, 23);
        panel.add(btnGhiDuLieuNoiTiep);
    }
}
